<template>
    <div class="page-container" v-loading="loading">
        <div class="page-filter-option mb-3">
            <el-form :inline="true" :model="filterOption" class="flex flex-wrap">
                <el-form-item>
                    <el-input :placeholder="$t('form.inputPlaceholder')" suffix-icon="el-icon-search" class="input-search-select"
                              v-model="filterOption.keyword" @change="filterChange" clearable>
                        <el-select slot="prepend" v-model="filterOption.keyword_column">
                            <el-option label="UID" value="uid"></el-option>
                            <el-option :label="$t('game.user.account_id')" value="account_id"></el-option>
                        </el-select>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <component-page-org-tree-option-check v-model="filterOption.plat_code" @change="filterChange"></component-page-org-tree-option-check>
                </el-form-item>
                <el-form-item>
                    <component-page-option-datetime-range type="datetimerange" align="center" defaultShortcuts
                                                          :init-last-day="1" v-model="filterOption.datetime" @change="filterChange">
                    </component-page-option-datetime-range>
                </el-form-item>
                <el-form-item>
                    <el-button @click="filterChange">{{ $t('action.search') }}</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="page-container-inner-fluid flex-auto flex flex-col">
            <component-page-content-resize v-slot="{ content }">
                <el-table style="width: 100%" :height="content.height"
                          :data="dataList"
                          :default-sort="{prop: filterOption.order_column, order: filterOption.order_key}"
                          @sort-change="filterOrderChange"
                          highlight-current-row>
                    <el-table-column min-width="150" label="UID" prop="uid">
                        <template slot-scope="scope">
                            <div v-if="scope.column.property != filterOption.keyword_column">{{ scope.row.uid }}</div>
                            <div v-else v-html="$options.filters.hsFilterKeyword(scope.row.uid, filterOption.keyword)"></div>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="200" :label="$t('platform.setting.tree.org')" prop="plat_code">
                        <template slot-scope="scope">{{ scope.row.org.name || scope.row.plat_code }}</template>
                    </el-table-column>
                    <el-table-column min-width="200" :label="$t('game.user.account_id')" prop="account_id" fixed="left">
                        <template slot-scope="scope">
                            <div v-if="scope.column.property != filterOption.keyword_column">{{ scope.row.account_id }}</div>
                            <div v-else v-html="$options.filters.hsFilterKeyword(scope.row.account_id, filterOption.keyword)"></div>
                        </template>
                    </el-table-column>
                    <el-table-column min-width="150" :label="$t('game.data.log-login.ip')" prop="ip"></el-table-column>
                    <el-table-column min-width="150" :label="$t('game.data.log-login.country')" prop="country"></el-table-column>
                    <el-table-column min-width="150" :label="$t('game.data.log-login.city')" prop="city"></el-table-column>
                    <el-table-column min-width="220" :label="$t('game.data.log-login.login_time')" prop="login_time" sortable="custom" :sort-orders="['descending', 'ascending']">
                        <template slot-scope="scope">
                            <component-page-timestamp :timestamp="scope.row.login_time"></component-page-timestamp>
                        </template>
                    </el-table-column>
                    <el-table-column width="100" :label="$t('game.data.log-login.operation')" align="right" fixed="right">
                        <template slot-scope="scope">
                            <span class="operation-options-icon">
                                <i class="el-icon-view" @click="drawerItemDetails(scope)"></i>
                            </span>
                        </template>
                    </el-table-column>
                </el-table>
            </component-page-content-resize>
            <div class="text-right p-4">
                <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                               :current-page="filterOption.page"
                               :page-size.sync="filterOption.page_size"
                               :total="dataMeta.total"
                               @size-change="filterChange"
                               @current-change="filterPageChange">
                </el-pagination>
            </div>
        </div>
        <item-details :visible.sync="drawer.visible.details" :data="itemCache"></item-details>
    </div>
</template>

<script>
import ItemDetails from './ItemDetails.vue'

export default {
    components: {
        ItemDetails,
    },
    data() {
        return {
            loading: false,
            filterOption: {
                plat_code: [],
                datetime: null,
                datetime_column: 'login_time',
                keyword: '',
                keyword_column: 'account_id',
                page: 1,
                page_size: 30,
                order: 'desc',
                order_key: 'descending',
                order_column: 'login_time',
            },
            dataMeta: {
                total: 0,
            },
            dataList: [],
            itemCache: {},
            drawer: {
                visible: {
                    details: false,
                }
            },
        }
    },
    methods: {
        clearFilterOption() {
            this.loading = true;
            this.filterOption.page = 1;
        },
        filterChange() {
            this.clearFilterOption();
            this.getDataList();
        },
        filterPageChange(page) {
            this.loading = true;
            this.filterOption.page = page;
            this.getDataList();
        },
        filterOrderChange(scope) {
            this.clearFilterOption();
            this.filterOption.order = scope.order == 'ascending' ? 'asc' : 'desc';
            this.filterOption.order_key = scope.order;
            this.filterOption.order_column = scope.prop;
            this.getDataList();
        },
        getDataList() {
            axios.get('/game/data/log-login/getList', {
                params: this.filterOption
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.dataMeta = response.data.resp_data.meta;
                    this.dataList = response.data.resp_data.data;
                } else {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                }
                this.loading = false;
            })
        },
        // 打开抽屉（详情）
        drawerItemDetails(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示抽屉
            this.drawer.visible.details = true;
        },
    },
}
</script>

<style lang="scss">

</style>
